﻿using System;
using System.Collections.Generic;
using PermitsAndVisasLeadsSyncher.Model;
using PermitsAndVisasLeadsSyncher.Service_Proxy;
using System.Linq;
using System.Xml;
using PermitsAndVisasLeadsSyncher.Helpers;

namespace PermitsAndVisasLeadsSyncher.Data_Gateway
{
    internal class LeadInfoGateway
    {
       public  string from = string.Empty;
       public  string lastLeadDate = string.Empty;


       public string getFromDate()
       {
           return from;
       }

       public string getLastLeadDate()
       {
           return lastLeadDate;
       }


       public void LoadleadInfoFromService()
       {
           LeadInfo leadsInfo = new LeadInfo();
           LeadServiceRequest leadServiceRequest = new LeadServiceRequest();
           XmlNodeList xmlNodeList1 = leadServiceRequest.GetLeadsInfo();
           foreach (XmlNode xmlNode in xmlNodeList1)
           {
               if (xmlNode.Attributes.Count > 0)
               {
                   if (xmlNode.Attributes["name"].Value == "firstlead")
                   {
                       leadsInfo.FirstLeadId = xmlNode.Attributes["id"].Value;
                       leadsInfo.FirstLeadDate = xmlNode.Attributes["date"].Value;
                   }
                   else if (xmlNode.Attributes["name"].Value == "lastlead")
                   {
                       leadsInfo.LastLeadId = xmlNode.Attributes["id"].Value;
                       leadsInfo.LastLeadDate = xmlNode.Attributes["date"].Value;
                   }
                   else if (xmlNode.Attributes["name"].Value == "totalleads")
                       leadsInfo.LeadsCount = xmlNode.Attributes["count"].Value;
               }
           }
           bool firstLeadInfoCheck = InfoProvider.GetFirstLeadInfoCheck();
           from = !firstLeadInfoCheck ? InfoProvider.FetchLastSucessfullLeadEDateFromDatabase() : leadsInfo.FirstLeadDate;
           lastLeadDate = leadsInfo.LastLeadDate;
           Console.WriteLine("Fetching the records from :{0}  to  {1}", (object)from, (object)lastLeadDate);
           if (from.Trim() == lastLeadDate.Trim())
           {
               Console.WriteLine("No Leads generated since last sync up with www.permitsandvisas.com");
               return;
           }
       }


    }
}
